


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > Ghost</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.npc</a>
</div>

<h1>Coverage Summary for Class: Ghost (nl.tudelft.jpacman.npc)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">Ghost</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/17)
  </span>
</td>
</tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$2cu3TjSW</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$4H89Rcr0</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$Br8z26ow</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$BThdKI7M</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$C4BZmyQc</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$Gej8rhBu</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$RwK9DQ1d</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$S18GhYlZ</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$thMsPSow</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$tYCJhXX0</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$VFoWXdDC</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$wgq70xmk</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$Yr4xwm7A</td>
  </tr>
  <tr>
    <td class="name">Ghost$MockitoMock$1526882914$auxiliary$yVzs0bxT</td>
  </tr>
<tr>
  <td class="name"><strong>Total</strong></td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/17)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.npc;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
<i class="no-highlight">4</i>&nbsp;import nl.tudelft.jpacman.board.Square;
<i class="no-highlight">5</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
<i class="no-highlight">6</i>&nbsp;import nl.tudelft.jpacman.sprite.Sprite;
<i class="no-highlight">7</i>&nbsp;
<i class="no-highlight">8</i>&nbsp;import java.util.ArrayList;
<i class="no-highlight">9</i>&nbsp;import java.util.List;
<i class="no-highlight">10</i>&nbsp;import java.util.Map;
<i class="no-highlight">11</i>&nbsp;import java.util.Optional;
<i class="no-highlight">12</i>&nbsp;import java.util.Random;
<i class="no-highlight">13</i>&nbsp;
<i class="no-highlight">14</i>&nbsp;/**
<i class="no-highlight">15</i>&nbsp; * A non-player unit.
<i class="no-highlight">16</i>&nbsp; *
<i class="no-highlight">17</i>&nbsp; * @author Jeroen Roosen
<i class="no-highlight">18</i>&nbsp; */
<i class="no-highlight">19</i>&nbsp;public abstract class Ghost extends Unit {
<i class="no-highlight">20</i>&nbsp;    /**
<i class="no-highlight">21</i>&nbsp;     * The sprite map, one sprite for each direction.
<i class="no-highlight">22</i>&nbsp;     */
<i class="no-highlight">23</i>&nbsp;    private final Map&lt;Direction, Sprite&gt; sprites;
<i class="no-highlight">24</i>&nbsp;
<i class="no-highlight">25</i>&nbsp;    /**
<i class="no-highlight">26</i>&nbsp;     * The base move interval of the ghost.
<i class="no-highlight">27</i>&nbsp;     */
<i class="no-highlight">28</i>&nbsp;    private final int moveInterval;
<i class="no-highlight">29</i>&nbsp;
<i class="no-highlight">30</i>&nbsp;    /**
<i class="no-highlight">31</i>&nbsp;     * The random variation added to the {@link #moveInterval}.
<i class="no-highlight">32</i>&nbsp;     */
<i class="no-highlight">33</i>&nbsp;    private final int intervalVariation;
<i class="no-highlight">34</i>&nbsp;
<i class="no-highlight">35</i>&nbsp;    /**
<i class="no-highlight">36</i>&nbsp;     * Calculates the next move for this unit and returns the direction to move
<i class="no-highlight">37</i>&nbsp;     * in.
<i class="no-highlight">38</i>&nbsp;     * &lt;p&gt;
<i class="no-highlight">39</i>&nbsp;     * Precondition: The NPC occupies a square (hasSquare() holds).
<i class="no-highlight">40</i>&nbsp;     *
<i class="no-highlight">41</i>&nbsp;     * @return The direction to move in, or &lt;code&gt;null&lt;/code&gt; if no move could
<i class="no-highlight">42</i>&nbsp;     * be devised.
<i class="no-highlight">43</i>&nbsp;     */
<i class="no-highlight">44</i>&nbsp;    public Direction nextMove() {
<b class="nc"><i class="no-highlight">45</i>&nbsp;        return nextAiMove().orElseGet(this::randomMove);</b>
<i class="no-highlight">46</i>&nbsp;    }
<i class="no-highlight">47</i>&nbsp;
<i class="no-highlight">48</i>&nbsp;    /**
<i class="no-highlight">49</i>&nbsp;     * Tries to calculate a move based on the behaviour of the npc.
<i class="no-highlight">50</i>&nbsp;     *
<i class="no-highlight">51</i>&nbsp;     * @return an optional containing the move or empty if the current state of the game
<i class="no-highlight">52</i>&nbsp;     * makes the ai move impossible
<i class="no-highlight">53</i>&nbsp;     */
<i class="no-highlight">54</i>&nbsp;    public abstract Optional&lt;Direction&gt; nextAiMove();
<i class="no-highlight">55</i>&nbsp;
<i class="no-highlight">56</i>&nbsp;    /**
<i class="no-highlight">57</i>&nbsp;     * Creates a new ghost.
<i class="no-highlight">58</i>&nbsp;     *
<i class="no-highlight">59</i>&nbsp;     * @param spriteMap         The sprites for every direction.
<i class="no-highlight">60</i>&nbsp;     * @param moveInterval      The base interval of movement.
<i class="no-highlight">61</i>&nbsp;     * @param intervalVariation The variation of the interval.
<i class="no-highlight">62</i>&nbsp;     */
<b class="nc"><i class="no-highlight">63</i>&nbsp;    protected Ghost(Map&lt;Direction, Sprite&gt; spriteMap, int moveInterval, int intervalVariation) {</b>
<b class="nc"><i class="no-highlight">64</i>&nbsp;        this.sprites = spriteMap;</b>
<b class="nc"><i class="no-highlight">65</i>&nbsp;        this.intervalVariation = intervalVariation;</b>
<b class="nc"><i class="no-highlight">66</i>&nbsp;        this.moveInterval = moveInterval;</b>
<b class="nc"><i class="no-highlight">67</i>&nbsp;    }</b>
<i class="no-highlight">68</i>&nbsp;
<i class="no-highlight">69</i>&nbsp;    @Override
<i class="no-highlight">70</i>&nbsp;    public Sprite getSprite() {
<b class="nc"><i class="no-highlight">71</i>&nbsp;        return sprites.get(getDirection());</b>
<i class="no-highlight">72</i>&nbsp;    }
<i class="no-highlight">73</i>&nbsp;
<i class="no-highlight">74</i>&nbsp;    /**
<i class="no-highlight">75</i>&nbsp;     * The time that should be taken between moves.
<i class="no-highlight">76</i>&nbsp;     *
<i class="no-highlight">77</i>&nbsp;     * @return The suggested delay between moves in milliseconds.
<i class="no-highlight">78</i>&nbsp;     */
<i class="no-highlight">79</i>&nbsp;    public long getInterval() {
<b class="nc"><i class="no-highlight">80</i>&nbsp;        return this.moveInterval + new Random().nextInt(this.intervalVariation);</b>
<i class="no-highlight">81</i>&nbsp;    }
<i class="no-highlight">82</i>&nbsp;
<i class="no-highlight">83</i>&nbsp;    /**
<i class="no-highlight">84</i>&nbsp;     * Determines a possible move in a random direction.
<i class="no-highlight">85</i>&nbsp;     *
<i class="no-highlight">86</i>&nbsp;     * @return A direction in which the ghost can move, or &lt;code&gt;null&lt;/code&gt; if
<i class="no-highlight">87</i>&nbsp;     * the ghost is shut in by inaccessible squares.
<i class="no-highlight">88</i>&nbsp;     */
<i class="no-highlight">89</i>&nbsp;    protected Direction randomMove() {
<b class="nc"><i class="no-highlight">90</i>&nbsp;        Square square = getSquare();</b>
<b class="nc"><i class="no-highlight">91</i>&nbsp;        List&lt;Direction&gt; directions = new ArrayList&lt;&gt;();</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;        for (Direction direction : Direction.values()) {</b>
<b class="nc"><i class="no-highlight">93</i>&nbsp;            if (square.getSquareAt(direction).isAccessibleTo(this)) {</b>
<b class="nc"><i class="no-highlight">94</i>&nbsp;                directions.add(direction);</b>
<i class="no-highlight">95</i>&nbsp;            }
<i class="no-highlight">96</i>&nbsp;        }
<b class="nc"><i class="no-highlight">97</i>&nbsp;        if (directions.isEmpty()) {</b>
<b class="nc"><i class="no-highlight">98</i>&nbsp;            return null;</b>
<i class="no-highlight">99</i>&nbsp;        }
<b class="nc"><i class="no-highlight">100</i>&nbsp;        int i = new Random().nextInt(directions.size());</b>
<b class="nc"><i class="no-highlight">101</i>&nbsp;        return directions.get(i);</b>
<i class="no-highlight">102</i>&nbsp;    }
<i class="no-highlight">103</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
